package com.itheima.controller;

import com.itheima.pojo.Result;
import com.itheima.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Slf4j
@RestController
public class UploadController {

    @Autowired
    private AliOSSUtils aliOSSUtils;
    //本地存储
//    @PostMapping("/upload")
//    public Result upLoad(String username, Integer age, MultipartFile image) throws IOException {
//        log.info("文件上传: {}, {}, {}",username, age, image);
//
//        //获取原始文件名
//        String originalFilename = image.getOriginalFilename();
//
//        //构建新的文件名
//        int index = originalFilename.lastIndexOf(".");
//        String extendName = originalFilename.substring(index);
//        String newFileName = UUID.randomUUID().toString() + extendName;
//
//        //文件上传的本地存储 D:\develop\image\
//        image.transferTo(new File("D:\\develop\\image\\" + newFileName)); //文件的存放路径
//
//        return Result.success();
//    }

    //阿里OSS存储
    @PostMapping("/upload")
    public Result upload(MultipartFile image) throws IOException {
        /*打印文件上传信息*/
        log.info("文件上传，文件名:{}",image.getOriginalFilename());

        /*调用阿里OSS工具类进行文件上传*/
        String url = aliOSSUtils.upload(image);

        /*打印文件成功上传信息*/
        log.info("文件成功上传,文件访问的url:{}",url);
        return Result.success(url);
    }

}
